<template>
  <div class="holiday-setting">
    <div class="header">
      <div class="left">
        <el-button type="primary" @click="handleAdd">+ 新建</el-button>
      </div>
      <div class="right">
        <el-input
          v-model="searchQuery"
          placeholder="通过节假日名称搜索"
          prefix-icon="el-icon-search"
          clearable
          @clear="handleSearch"
          @input="handleSearch"
        />
      </div>
    </div>

    <el-table
      :data="tableData"
      border
      style="width: 100%"
      v-loading="loading"
    >
      <el-table-column
        prop="name"
        label="节假日名称"
        min-width="150">
      </el-table-column>
      <el-table-column
        prop="country"
        label="国家/地区"
        width="120">
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '已启用' ? 'success' : 'info'">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="creator"
        label="创建人"
        width="120">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="180">
      </el-table-column>
      <el-table-column
        prop="updater"
        label="更新人"
        width="120">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        label="更新时间"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作"
        width="150"
        fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新建/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="holidayForm" :rules="rules" ref="holidayForm" label-width="100px">
        <el-form-item label="节假日名称" prop="name">
          <el-input v-model="holidayForm.name" placeholder="请输入节假日名称"></el-input>
        </el-form-item>
        <el-form-item label="国家/地区" prop="country">
          <el-select v-model="holidayForm.country" placeholder="请选择国家/地区">
            <el-option label="中国大陆" value="中国大陆"></el-option>
            <el-option label="中国香港" value="中国香港"></el-option>
            <el-option label="中国澳门" value="中国澳门"></el-option>
            <el-option label="中国台湾" value="中国台湾"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="holidayForm.status"
            active-text="启用"
            inactive-text="禁用">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'HolidaySetting',
  data() {
    return {
      searchQuery: '',
      loading: false,
      tableData: [
        {
          name: '中国大陆法定节假日',
          country: '中国大陆',
          status: '已启用',
          creator: '系统用户',
          createTime: '2023-01-01 00:00:00',
          updater: '系统用户',
          updateTime: '2023-01-01 00:00:00'
        }
      ],
      dialogVisible: false,
      dialogTitle: '',
      holidayForm: {
        name: '',
        country: '',
        status: true
      },
      rules: {
        name: [
          { required: true, message: '请输入节假日名称', trigger: 'blur' }
        ],
        country: [
          { required: true, message: '请选择国家/地区', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    handleSearch() {
      // TODO: 实现搜索功能
      console.log('搜索关键词:', this.searchQuery)
    },
    handleAdd() {
      this.dialogTitle = '新建节假日'
      this.holidayForm = {
        name: '',
        country: '',
        status: true
      }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑节假日'
      this.holidayForm = {
        name: row.name,
        country: row.country,
        status: row.status === '已启用'
      }
      this.dialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('确认删除该节假日吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // TODO: 调用删除API
        this.$message.success('删除成功')
      }).catch(() => {})
    },
    submitForm() {
      this.$refs.holidayForm.validate((valid) => {
        if (valid) {
          // TODO: 调用保存API
          this.$message.success('保存成功')
          this.dialogVisible = false
        }
      })
    }
  }
}
</script>

<style scoped>
.holiday-setting {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.right {
  display: flex;
  gap: 10px;
}

.el-input {
  width: 300px;
}
</style>